package com.cloud.bc.controller.pay;

import com.cloud.bc.config.Config;
import com.cloud.bc.controller.BaseController;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.BcPaymenorders;
import com.cloud.bc.model.enums.PayStatusEnum;
import com.cloud.bc.model.enums.PayTypeEnum;
import com.cloud.bc.service.BcPayService;
import com.cloud.bc.service.BcPaymenordersService;
import com.cloud.bc.service.BcUsersService;
import com.cloud.bc.util.BcUtils;
import com.cloud.common.util.R;
import com.cloud.hpj.HpjSuccessResponse;
import com.cloud.model.HpjPayResponse;
import com.cloud.model.PayRequest;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@AllArgsConstructor
@Controller
@RequestMapping("pay/hpj/weixin")
public class HpjWeiXinController extends BaseController {
    private final BcUsersService bcUsersService;
    private final BcPaymenordersService bcPaymenordersService;
    private final BcPayService bcPayService;
    private final Config config;
    private final BcUtils bcUtils;

    /**
     * 收银台
     */
    @GetMapping("qrcode/{orderNo}")
    public ModelAndView qrcode(HttpServletRequest request, HttpServletResponse response, @PathVariable("orderNo") String orderNo) {
        ModelAndView view = baseView(request, "微信支付-51找朋友", "pc/pay/hpj/qrcode");
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        String userAgent = request.getHeader("user-agent").toLowerCase();
        if (userInfo == null) {
            if (userAgent.contains("micromessenger")) {
                return new ModelAndView("redirect:/m/login");
            } else {
                return new ModelAndView("redirect:/login");
            }
        }
        BcPaymenorders bcPaymenordersReq = new BcPaymenorders();
        bcPaymenordersReq.setOrderId(orderNo);
        bcPaymenordersReq.setUserId(userInfo.getId());
        BcPaymenorders bcPaymenorders = bcPaymenordersService.selectOne(bcPaymenordersReq);
        PayRequest payRequest = new PayRequest();
        payRequest.setTradeOrderId(orderNo);
        payRequest.setGoodsName(bcPaymenorders.getItemName());
        payRequest.setPayAmount(bcPaymenorders.getPayAmount() + "");
//        payRequest.setMemo(bOrderResult.getAId().toString());
        HpjPayResponse hpjPayResponse = bcPayService.createPay(request, payRequest);
        if (userAgent.contains("micromessenger")) {
//如果是微信就用jsapi,如果是微信就返回跳转链接地址
            //跳转到第三方收款页面
            return new ModelAndView("redirect:" + hpjPayResponse.getUrl());
        }
        String qrcode = hpjPayResponse.getUrl_qrcode();
        view.addObject("data", bcPaymenorders);
        view.addObject("successUrl", config.getWebSite() + "center");
        view.addObject("qrcode", qrcode);
        return view;
    }

    @ResponseBody
    @GetMapping("check/{orderNo}")
    public R check(HttpServletRequest request,
                   HttpServletResponse response,
                   @PathVariable("orderNo") String orderNo) {
        R r = new R();
        PayStatusEnum payStatusEnum = bcPayService.check(orderNo);
        r.setData(payStatusEnum.getCode());
        return r;
    }

    //支付成功回调
    @SneakyThrows
    @ResponseBody
    @PostMapping("paySuccess")
    public String paySuccess(HttpServletRequest request, HttpServletResponse response,
                             HpjSuccessResponse hpjSuccessResponse) {
        if (bcPayService.notify(hpjSuccessResponse)) {
            String orderNo = hpjSuccessResponse.getTrade_order_id();
            BcPaymenorders bcPaymenorders = bcPaymenordersService.selectOne(new BcPaymenorders().setOrderId(orderNo));
            bcPaymenorders.setPayWay(PayTypeEnum.HPJ.getCode() + "");
            bcPaymenordersService.createNotifyOrder(bcPaymenorders.getOrderId(), bcPaymenorders.getPayOrderId());
            return "success";
        }
        return "fail";
    }
}
